<?php
$pagelen = 2;//จำนวนที่แสดงผลข้อมูลต่อหน้า
$range = 1;
$page_start = 0;
$page_end = 30;
$pblog = $_REQUEST['pblog']; //รับค่าตัวแปร pagepblogแบบ get
if(empty($pblog) || ($pblog < 1)){ $pblog=1; } //ถ้าตัวแปรเพจยังไม่มี ให้ค่าเริ่มต้นของ $page เป็น pblog

if (empty($cid)){	
	$get_blogAll = $inancm->select_db("select * from inancm_blog where blog_show = 1");	
} else {
	$get_blogAll = $inancm->select_db("select * from inancm_blog where blog_show = 1 and cat_id = $cid");	
}
$totals1 = count($get_blogAll);
$totalpage = ceil($totals1 / $pagelen);

if ($pblog >= $totalpage) {
	$pblog = $totalpage;
} 

$goto = ($pblog-1) * $pagelen; // หาหน้าที่จะกระโดดไป
$start = $pblog - $range;
$end = $pblog + $range;
if ($start <= 1) { $start = 1;}
if ($end >= $totalpage) { $end = $totalpage; } 
if ($goto < 0) { $goto = $totalpage; } 

if (empty($cid)){			
	$get_blog2 = $inancm->select_db("select * from inancm_blog inner join inancm_category on inancm_blog.cat_id = inancm_category.cat_id where blog_show = 1 order by blog_id desc LIMIT $goto,$pagelen");
} else {
	$get_blog2 = $inancm->select_db("select * from inancm_blog inner join inancm_category on inancm_blog.cat_id = inancm_category.cat_id where blog_show = 1 and inancm_blog.cat_id = $cid order by blog_id desc LIMIT $goto,$pagelen");
}
?>

<!-- Start Main Body Wrap -->
<div id="main-wrap">
    
    <!-- Start Left Section -->
    <div class="leftsection leftsectionalt">
    
    	<?php 
		$i = 0;
		foreach($get_blog2 as $val) { 
			$i++;
			$date = date_create($val['datetime']);
			$date_show = date_format($date, 'F jS Y');
			?>
            <!-- Start Blog Post -->
            <?php if ($i == 1 ) { ?> <div class="blogwrapstart"> <?php } else { ?><div class="blogwrap"><?php } ?>
               
                <div class="blogtitle"><h3><a href="?page=<?=$page?>&bid=<?=$val['blog_id'];?>" title="5 ways to get top of google">
                    <?=$val['blog_title'];?></a></h3></div>
                
                <div class="blogbody">
                    <div class="blogimage"><a href="?page=<?=$page?>&bid=<?=$val['blog_id'];?>">
                        <img src="images/blog/<?=$val['blog_image'];?>" alt="<?=$val['blog_title'];?>"></a>
                    </div>
                  
                    <div class="bloginfo">                
                      <p class="usericon">Posted By: <span><?=$val['blog_post'];?></span></p>
                      <p class="calendericon">On: <span><?=$date_show;?></span></p>
                      <p class="settingsicon">Category: <span><?=$val['cat_name'];?></span></p>
                      <p class="tagicon">Tags: <span><?=$val['blog_tag'];?></span></p>                
                    </div>              
                    <div class="blogtext">                
                        <p><?=$val['blog_des'];?></p>                
                        <p class="blogbutton"><a href="?page=<?=$page?>&bid=<?=$val['blog_id'];?>" title="Read More" class="smallsmoothrectange orangebutton">Read More</a></p>                
                    </div>              
                </div>            
                <span class="box-arrow"></span>        
            </div>
            <!-- End Blog Post -->
        <?php } ?>
        
      
      	<?php if ($totalpage > 1) { ?>
        <!-- Start Pagination -->
        <div class="blogwrap">        
            <div class="blogpagination">            
                <ul>                
                    <li><a href="?page=<?=$page?>&pblog=1&cid=<?=$cid?>" title="Start" class="normal">Start</a></li>
                    <li><a href="?page=<?=$page?>&pblog=<?=$np-1?>&cid=<?=$cid?>" title="Previous" class="normal">&lt;Prev</a></li>
                    <?php for ($np=1; $np<=$totalpage; $np++) {?>
                    	<?php if ($np == $pblog) { ?>
                    		<li><a href="?page=<?=$page?>&pblog=<?=$np?>&cid=<?=$cid?>" title="<?=$np;?>" class="normalactive"><?=$np;?></a></li>
                        <?php } else { ?>
                    		<li><a href="?page=<?=$page?>&pblog=<?=$np?>&cid=<?=$cid?>" title="<?=$np?>" class="normal"><?=$np?></a></li> 
                        <?php } ?>
                    <?php } ?>    
                    <li><a href="?page=<?=$page?>&pblog=<?=$np+1?>&cid=<?=$cid?>" title="Next" class="normal">Next&gt;</a></li>
                    <li><a href="?page=<?=$page?>&pblog=<?=$totalpage?>&cid=<?=$cid?>" title="Last" class="normal">Last</a></li>
                </ul>            
            </div>             
            <span class="box-arrow"></span>
        </div> 
        <!-- End Pagination -->
        <?php } ?>
    
    </div>
    <!-- End Left Section -->
    
    <!-- Start Right Section -->
    <?php include ('modules/mod_blog/blog.right.php'); ?>
    <!-- End Right Section -->

</div>
<!-- End Main Body Wrap -->